package com.example.network;

import com.google.gson.GsonBuilder;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by zhaoyulu on 5/9/18.
 */

public class getRetrofitService {

    private static RetrofitService retrofitService;

    public static RetrofitService getService(){
        if(retrofitService!=null) return retrofitService;


        Interceptor appInterceptor = new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request();
                //---------请求之前------------
                Response response = chain.proceed(request);
                //---------请求之后------------
                return response;
            }

        };

        OkHttpClient client= new OkHttpClient
                .Builder()
                .addInterceptor(appInterceptor)//Application拦截器
                .build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://101.132.121.32:8080/ServletTest/")
                .client(client)
                .addConverterFactory(GsonConverterFactory.create(new GsonBuilder().create()))
                .build();
        retrofitService= retrofit.create(RetrofitService.class);



        return retrofitService;
    }

}
